package com.xgq.drink.controller;

import com.xgq.drink.entity.DiscountClaim;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IDiscountClaimService;
import com.xgq.drink.utils.JwtTokenUtil;
import com.xgq.drink.utils.RUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/discount")
@Tag(name = "优惠券领取模块", description = "优惠券领取相关接口")
public class DiscountClaimController {

    @Autowired
    private IDiscountClaimService discountClaimService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @GetMapping("/list")
    @Operation(summary = "分页查询用户的所有优惠券",
            responses = {
                    @ApiResponse(responseCode = "200", description = "查询成功",
                            content = @Content(schema = @Schema(implementation = List.class))),
                    @ApiResponse(responseCode = "400", description = "查询失败",
                            content = @Content(schema = @Schema(implementation = R.class)))
            })
    public R getDiscountClaimsByUserId(@RequestParam(required = false) String discountType,
                                       @RequestParam(required = false) Integer status,
                                       @RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "10") int size, HttpServletRequest request) {
        try {
            List<DiscountClaim> claims = discountClaimService.getDiscountClaimsByUserId(jwtTokenUtil.getUserIdFromRequest(request), discountType, status, current, size).getRecords();
            System.out.println("=================");
            System.out.println(claims);
            return RUtils.success("查询成功", claims);
        } catch (Exception e) {
            return RUtils.fail("查询失败: " + e.getMessage());
        }
    }

    @PostMapping("/claim")
    @Operation(summary = "用户领取优惠券",
            responses = {
                    @ApiResponse(responseCode = "200", description = "领取成功",
                            content = @Content(schema = @Schema(implementation = DiscountClaim.class))),
                    @ApiResponse(responseCode = "400", description = "领取失败",
                            content = @Content(schema = @Schema(implementation = R.class)))
            })
    public R claimDiscount(@RequestParam Long discountId,HttpServletRequest request) {
        Long userId = jwtTokenUtil.getUserIdFromRequest(request);
        try {
            DiscountClaim claim = discountClaimService.claimDiscount(userId, discountId);
            return RUtils.success("领取成功", claim);
        } catch (Exception e) {
            return RUtils.fail("领取失败: " + e.getMessage());
        }
    }
}